﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DiscoveryLogic.Analytics.BusinessLogic.Entities;
using DiscoveryLogic.Analytics.DataObjects;
using DiscoveryLogic.Analytics.UI.Common.Events;
using DiscoveryLogic.Common.Utilities;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Wpf.Events;
using EntityBase=DiscoveryLogic.Analytics.DataObjects.EntityBase;

namespace DiscoveryLogic.Analytics.Modules.Entities
{
    public class GrantTreePresenter
    {
        private GrantTreeView _View;
		/// <summary></summary>
		public GrantTreeView View
		{
			get
			{
				return this._View;
			}
			set
			{
				this._View = value;
				if(this._View !=null)
				{
					this._View.GrantDataSetSelected += new GrantTreeView.OnGrantDataSetSelected(SelectGrantDataSet);
				    this._View.PopulateTreeView(this.ReadGrantDataSets());
				}
			}
		}

		private readonly IEventAggregator _EventAggregator;

        public GrantTreePresenter(IEventAggregator eventAggregator)
		{
			this._EventAggregator = eventAggregator;
		}

		public void SelectGrant(GrantDO grant)
		{
			if(grant !=null)
			{
				this._EventAggregator.GetEvent<GrantSelectedEvent>().Publish(grant);
			}
		}

        private void SelectGrantDataSet(string dataSetName)
        {
            if (!string.IsNullOrEmpty(dataSetName))
            {
                this._EventAggregator.GetEvent<GrantDataSetSelectedEvent>().Publish(dataSetName);
            }
        }

        private List<EntityDataSet> ReadGrantDataSets()
		{
            //List<EntityDataSet> sets=new List<EntityDataSet>();
            //Dictionary<string, List<EntityBase>> grantDataSets = EntityManager.GetEntityDataSets(EntityType.Grants);
            //if(grantDataSets !=null && grantDataSets.Count>0)
            //{
            //    foreach(string setName in grantDataSets.Keys)
            //    {
            //        EntityDataSet dataSet = new EntityDataSet();
            //        dataSet.SourceType = SetDataSourceType.DB;
            //        dataSet.EntityType = EntityType.Grants;
            //        dataSet.DataSetName = setName;
            //        dataSet.EntityCount = grantDataSets[setName].Count;
            //        sets.Add(dataSet);
            //    }
            //}
            List<EntityDataSet> sets = EntityManager.GenerateDataSetNames(EntityType.Grants,"");
            return sets;
		}
    }
}
